/* ------------------------------------------------------------------------
	Author: SlopeTracker (http://www.pluginexpert.com)
	Version: 1.0
------------------------------------------------------------------------- */
var serverUrl = 'http://demo.socialmediatoolbox.no';
var stToolbar = {
    loadPage: function(action, data_post, message, callback_function, page_name) {
        var post_data = "ajax=1&action="+action;
        if(page_name == '' || page_name == null){
            page_name = "stToolbar";
        }
        post_data = "page="+page_name+'&'+post_data;
        if(data_post != '' || data_post != null){
            for( var key in data_post){
                if(key == 'sortKey')
                    post_data +="&sort="+data_post[key];
                else
                    post_data +="&"+key+"="+data_post[key];
            }
        }
        //alert(post_data);
        jQuery.ajax({
            type: "POST",
            url: serverUrl,
            data: post_data,
            cache: false,
            beforeSend: function(){
//                if(jQuery("#loading").css('display') == 'none'){
//                    jQuery.blockUI({
//                        message: jQuery('#loading'),
//                        css: {
//                        border: 'none',
//                        padding: '15px',
//                        backgroundColor: '#000',
//                        '-webkit-border-radius': '10px',
//                        '-moz-border-radius': '10px',
//                        opacity: .5,
//                        color: '#fff'
//                    }});
//                }
//                jQuery("#loading").fadeIn("fast");
//                jQuery("#inner_content").fadeOut("fast");
            }, //show loading just when link is clicked
            complete: function(){
//                jQuery("#loading").fadeOut("fast");
//                jQuery.unblockUI();
            }, //stop showing loading when the process is complete
            success: function(data){
                jQuery("#inner_content").html(data);
                jQuery("#inner_content").append('<input type="hidden" value="'+action+'" id="current_action" />');
                if(message){
                    stToolbar.sendMessage(message);
                }
                if(callback_function){
                    callback_function.call(this, data_post);
                }
                jQuery("#inner_content").fadeIn("normal");

            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
//                setTimeout(jQuery.unblockUI, 2000);
            }
        });
		stToolbar.pagingCallbackFunction = function(data) {};
		stToolbar.sortCallbackFunction = function(data) {};
    },

    load_tooltip: function(id){
        jQuery(document).ready(function() {
            if(id == '')
                jQuery("#inner_content").tooltip();
            else
                jQuery(id).tooltip();
        });
    },
    
    sendMessage: function(data){
        jQuery(document).ready(function(){
            try{
                data = eval('('+data+')');
            }catch (e){}

            if(typeof(data)=='string'){
                var message = data;
                var status = 'message';
            }else{
                message = data['message'];
                status = data['status'];
            }
            if(status == 'success'){
                jQuery('.notification' ).removeClass('notification_error');
                jQuery('.notification' ).removeClass('notification_message');
                jQuery('.notification' ).addClass('notification_success');
                jQuery('.notification .text strong').html('Success!');
            }else if(status == 'message'){
                jQuery('.notification' ).removeClass('notification_error');
                jQuery('.notification' ).removeClass('notification_success');
                jQuery('.notification' ).addClass('notification_message');
                jQuery('.notification .text strong').html('Message!');
            }else{
                jQuery('.notification' ).removeClass('notification_message');
                jQuery('.notification' ).removeClass('notification_success');
                jQuery('.notification' ).addClass('notification_error');
                jQuery('.notification .text strong').html('Error!');
            }
            jQuery('.notification .text span').html(message);
            jQuery('.notification').fadeIn('slow');

            jQuery('.notification').click(function() {
                jQuery(this).fadeOut(800);
            });
            window.setTimeout(function(){
               jQuery('.notification').fadeOut();
            }, 2500);
        });

    },

    confirmModal: function(message, btnText, data_post, callbackFunction){
        jQuery( "#dialog-confirm" ).html("<p>"+message+"</p>");
        jQuery( "#dialog-confirm" ).dialog({
            title: 'Confirm',
            resizable: false,
            modal: true,
            show: "slide",
            hide: "slide",
            buttons: [
            {
                text: btnText,
                click: function() {
                    jQuery( this ).dialog( "close" );
                    var current_action = jQuery('#current_action').val();
                    if(callbackFunction == '' || callbackFunction == null)
                        stToolbar.loadPage(current_action, data_post);
                    else
                        eval(callbackFunction);
                }
            },
            {
                text: "Cancel",
                click: function() {
                    jQuery( this ).dialog( "close" );
                }
            }
            ]
        });
    }

};

jQuery(document).ready(function() {
    var data_post = new Array();
    data_post['domain'] = document.domain;
    stToolbar.loadPage('showToolBar', data_post);
})